<?php
include('class/class_connect.php');
include('class/class_page.php');
include('class/city_rus.php');
include('class/class_form.php');
include('class/class_checkGetData.php');
include('includes/rubriks.php');
foreach($_GET as $key=>$val)
{
	$_GET[$key]=checkGetData::safetyCheck($val);
}
foreach($_POST as $key=>$val)
{
	$_POST[$key]=checkGetData::safetyCheck($val);
}
$aCheckData=new checkGetData('ma');
$aCheckData->checkCity($_GET['city']);
$aCheckData->checkTableId($_GET['city'], $_GET['id']);
session_start();
if(!$_SESSION['login'])
{
	header('location: registr.php?city='.$_GET['city']);
	exit;
}
$aConnect=new connect('ma');
$row=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_GET['id']."'"));
$aConnect=new connect('m');
$r=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$row['malls_id']."'"));
if(strtolower($r['user'])!=strtolower($_SESSION['login']))
{
	echo ('
	<center>
		<h1>У Вас нет доступа к запрашиваемой странице!</h1>
		<a href="index.php">На главную</a>
		</center>
	');
	exit;
}
$aConnect=new connect('ma');
$aForm=new form();
$warning=false;
if($_POST['undoFirms'])unset($_POST);
if($_POST['editActs'])
{
	$warning=false;
	if($_POST['name'])
	{
		$warning=$aForm->checkInput($_POST['name'], 'Заголовок', 'name', $row['name'], 80, 3, "(.)", "", '');
		if(!$warning)$data['name']=$_POST['name'];
	}
	if($_POST['stext'] && !$warning)
	{
		$warning=$aForm->checkInput($_POST['stext'], 'Текст', 'stext', $row['stext'], 1000, 10, "(.)", "", '');
		if(!$warning)$data['stext']=$_POST['stext'];
	}
	$data['data']=$_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'].' '.$_POST['hour'].':'.$_POST['minute'].':00';
	if(!$warning)
	{
		if($data)//сделаны изменения
		{
			foreach($data as $r=>$v)
			{
				$dataTo[]=$r."='".$v."'";
			}
			if($dataTo)$dataToUpdate=implode(', ', $dataTo);
		}
		if($dataToUpdate)
			$aConnect->update_data($_GET['city'], $dataToUpdate, "WHERE id='".$_GET['id']."'"); //обновляем данные фирмы
		if($_POST['imgName'])
		{			if(!file_exists('pictures/mallsactspics/'.$_GET['city'].'/'))
				mkdir('pictures/mallsactspics/'.$_GET['city'].'/', 0777, true);
			if(file_exists($_POST['imgName'].'.jpg'))
			{
				rename($_POST['imgName'].'.jpg', 'pictures/mallsactspics/'.$_GET['city'].'/'.$row['id'].'.jpg');
			}
		}
		header('location: mallscabinet.php?city='.$_GET['city'].'&id='.$row['malls_id']);
	}
}
if($_POST['deleteActs'])
{
	$query="DELETE from ".$_GET['city']." WHERE id='".$_GET['id']."'";
	mysql_query($query) or die(mysql_error());
	if(file_exists('pictures/mallsactspics/'.$_GET['city'].'/'.$row['id'].'.jpg'))
		unlink('pictures/mallsactspics/'.$_GET['city'].'/'.$row['id'].'.jpg');
	header('location: mallscabinet.php?city='.$_GET['city'].'&id='.$row['firms_id']);

}
$aConnect=new connect('ma');
$row=mysql_fetch_array($aConnect->select_all($_GET['city'], "WHERE id='".$_GET['id']."'"));
$aForm=new  form();
class aFirmPage extends page
{
	var $warning;
	var $row=array();
	function __construct($warning, $row=array())
	{
		$this->warning=$warning;
		$this->row=$row;
		parent::__construct();
	}
	function title()
	{
		echo 'Редактировать Событие';
	}
	function maincontent()
	{
		include('includes/monthes.php');
		$cur_date=date_create($this->row['data']);
		$cur_day=date_format($cur_date, 'd');
		$cur_month=date_format($cur_date, 'm');
		$cur_year=date_format($cur_date, 'Y');
		$cur_hour=date_format($cur_date, 'H');
		$cur_minute=date_format($cur_date, 'i');
		$aForm=new  form();
		$aForm->includeStyle();
		$aForm->includeScript();
		$imgName='temp'.rand();
		if(file_exists('pictures/mallsactspics/'.$_GET['city'].'/'.$this->row['id'].'.jpg'))
			 $avatar='pictures/mallsactspics/'.$_GET['city'].'/'.$this->row['id'].'.jpg';
		if($_POST['imgName'])
		{
			$avatar=$_POST['imgName'].'.jpg';
		}
		echo ('
		<script type="text/javascript" src="scripts/editNews.js">
		</script>
		<script type="text/javascript" src="jquery/ajaxUpload.js">
  		</script>
		<link rel="stylesheet" type="text/css" href="style/editNews.css" />
		<div class="route">
			<div class="routeEl">
				<a href="pcabinet.php?city='.$_GET['city'].'">Личный кабинет</a>
			</div>
			<div class="routeEl">
				<a href="my_malls.php?city='.$_GET['city'].'">Мои Центры</a>
			</div>
			<div class="routeEl">
				<a href="mallscabinet.php?city='.$_GET['city'].'&id='.$this->row['malls_id'].'">'.$this->row['malls_name'].'</a>
			</div>
			<div class="routeEl">
				Редактировать Событие
			</div>
		</div>
		  <div class="form" align="center">
		  <h3>Редактировать Событие</h3>
		   <form method="post" id="editNews" name="editNews" action="" autocomplete="off">
		   		<input name="city" type="hidden" value="'.$_GET['city'].'" id="city">
		   		<center id="warning">&nbsp;'.$this->warning.'</center>
		   		<div class="myform">
			   		<div class="signature">
			   	 		Изображение:
			   	 	</div>
			   		<div id="avatar">
				     	<div id="upload">
				 			<span id="span" class="loadButton" imgName="'.$imgName.'">Изменить</span>
				 		</div>
				 		<div id="preview">
		   	 				<img src="'.$avatar.'" />
		   	 				<input type="hidden" name="imgName" value="'.$_POST['imgName'].'" />
		   	 				<span id="status"></span>
				    	</div>
			   	 	</div>
		   	 	</div>
		   	 	<div class="myform">
		   	 		<div class="signature">
	   					Дата
	   				</div>
	   				<select size="1" name="day">
	   				');
	   				for($i=1; $i<32; $i++)
	   				{
	   					$time=$i;
	   					if($time<10)$time='0'.$time;
	   					$selected=false;
	   					if($i==$cur_day)$selected="selected";
	   					echo ('
	  					<option value="'.$time.'" '.$selected.'>'.$time.'</option>
	  					');
	  				}
	  				echo ('
					</select>
	   				<select size="1" name="month">
	   				');
	   				for($i=1; $i<=12; $i++)
	   				{
	   					$selected=false;
	   					if($i==$cur_month)$selected="selected";
	   					echo ('
	  					<option value="'.$i.'" '.$selected.'>'.$monthes[$i].'</option>
	  					');
	  				}
	  				echo ('
					</select>
					<select size="1" name="year">
	   				');
	   				for($i=$cur_year; $i<($cur_year+3); $i++)
	   				{
	   					echo ('
	  					<option value="'.$i.'">'.$i.'</option>
	  					');
	  				}
	  				echo ('
					</select>
	   				<div class="signature">
	   					Время
	   				</div>
	   				<select size="1" name="hour">
	   				');
	   				for($i=0; $i<24; $i++)
	   				{
	   					$time=$i;
	   					if($time<10)$time='0'.$time;
	   					$selected=false;
	   					if($i==$cur_hour)$selected="selected";
	   					echo ('
	  					<option value="'.$time.'" '.$selected.'>'.$time.'</option>
	  					');
	  				}
	  				echo ('
					</select>
					<select size="1" name="minute">
	   				');
	   				for($i=0; $i<60; $i++)
	   				{
	   					$time=$i;
	   					if($time<10)$time='0'.$time;
	   					$selected=false;
	   					if($i==0)$selected="selected";
	   					echo ('
	  					<option value="'.$time.'" '.$selected.'>'.$time.'</option>
	  					');
	  				}
	  				echo ('
					</select>
		   	 	</div>
		   	 	<div class="myform">
			   	 	<div class="signature">
			   	 		Название:
			   	 	</div>
			   		<div id="name" class="edit" value="'.$this->row['name'].'">
			   			'.$this->row['name'].'<br /><but class="but">изменить</but>
			   		</div>
			   		<div class="signature">
			   	 		Текст:
			   	 	</div>
			   		<div id="text" class="edit" value="'.$this->row['stext'].'">
			   			'.$this->row['stext'].'<br /><but class="but">изменить</but>
			   		</div>
				</div>
                <input type="submit" value="Сохранить" class="button button-blue" name="editActs">
                <input type="submit" value="Удалить" class="button button-blue" name="deleteActs" id="deleteNews">
                <a href="mallscabinet.php?city='.$_GET['city'].'&id='.$this->row['firms_id'].'" class="button button-blue">Отменить</a>
		   </form>

		  </div>
		');
	}
}

$aPage=new aFirmPage($warning, $row);

?>